package com.beidouapp.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.beidouapp.dao.DictDao;
import com.beidouapp.domain.pojo.Dict;
import com.beidouapp.domain.pojo.DictType;
import com.beidouapp.exception.BusinessException;
import com.beidouapp.mapper.DictMapper;

@Repository
public class DictDaoImpl implements DictDao
{
	@Autowired
    private DictMapper m_dictMapper;

    public void setDictMapper (DictMapper dictMapper)
    {
        m_dictMapper = dictMapper;
    }

    @Override
    public Dict addDict (Dict dict) throws BusinessException
    {
        for(Dict dictTemp : this.findDictByDictTypeCode (dict.getDictTypeCode ()))
        {
            if(dict.getDictItemCode ().equals (dictTemp.getDictItemCode ()))
            {
                throw new BusinessException ("编码【"+dict.getDictItemCode ()+"】在字典类型【"+dict.getDictTypeCode ()+"】对应的字典编码条目中已经存在。");
            }
        }
        
        if (dict.getParentId () != null)
        {
            Dict parent = this.getDictById (dict.getParentId ());
            dict.setDictItemCodeFqn (parent.getDictItemCodeFqn () + "/" + dict.getDictItemCode ());
        }
        else
        {
            dict.setDictItemCodeFqn ("/" + dict.getDictItemCode ());
        }
        
        dict.setDictItemId (m_dictMapper.getNextDictId ());
        m_dictMapper.addDict (dict);
        return dict;
    }

    @Override
    public List <Dict> findChildrenDict (String itemCodeFqn, String dictTypeCode)
    {
        return m_dictMapper.findChildrenDict (itemCodeFqn, dictTypeCode);
    }
    
    @Override
    public List <Dict> findDictByDictTypeCode (String dictTypeCode)
    {
        return m_dictMapper.findDictByDictTypeCode (dictTypeCode);
    }

    @Override
    public List <Dict> findAllDict (boolean isExceptingNotLoadOnStart)
    {
        return m_dictMapper.findAllDict (isExceptingNotLoadOnStart);
    }

    @Override
    public Dict getDict (String itemCodeFqn, String dictTypeCode)
    {
        return m_dictMapper.getDict (itemCodeFqn, dictTypeCode);
    }

    @Override
    public Dict getDictById (Integer dictItemId)
    {
        return m_dictMapper.getDictById (dictItemId);
    }

    @Override
    @Transactional
    public void modifyDict (Dict dict) throws BusinessException
    {
        for(Dict dictTemp : this.findDictByDictTypeCode (dict.getDictTypeCode ()))
        {
            if(dict.getDictItemCode ().equals (dictTemp.getDictItemCode ()) && !dict.getDictItemId ().equals (dictTemp.getDictItemId ()))
            {
                throw new BusinessException ("新增失败，字典编码【"+dict.getDictItemCode ()+"】在字典类型【"+dict.getDictTypeCode ()+"】对应的字典编码条目中已经存在。");
            }
        }
        
        // 获取原FQN
        String oldDictFqn = dict.getDictItemCodeFqn ();

        // 根据上级ID情况，获取新FQN
        String newDictFqn;
        if (dict.getParentId () != null)
        {// 有上级的情况
            Dict parent = this.getDictById (dict.getParentId ());
            newDictFqn = parent.getDictItemCodeFqn () + "/" + dict.getDictItemCode ();
            dict.setDictItemCodeFqn (newDictFqn);
        }
        else
        {// 无上级的情况
            newDictFqn = "/" + dict.getDictItemCode ();
            dict.setDictItemCodeFqn (newDictFqn);
        }
        
        // 如果新旧FQN不相等，则需要大量修改当前节点子集的FQN
        if (!oldDictFqn.equals (newDictFqn))
        {
            List <Dict> children = m_dictMapper.findChildrenDict (oldDictFqn, dict.getDictTypeCode ());
            for (Dict child : children)
            {
                String newChildFqn = child.getDictItemCodeFqn ().replaceFirst (oldDictFqn, newDictFqn);
                child.setDictItemCodeFqn (newChildFqn);
                m_dictMapper.modifyDict (child);
            }
        }
        // 最后修改本身的FQN
        m_dictMapper.modifyDict (dict);
    }

    @Override
    public void removeDict (Integer dictItemId) throws BusinessException
    {
        Dict dict = getDictById (dictItemId);
        if(dict.getChildCount () != 0)
        {
            throw new BusinessException ("字典编码条目【"+dict.getDictItemCode ()+"】存在下级字典编码条目，请先移除子条目后再执行此操作。");
        }
        m_dictMapper.removeDict (dictItemId);
    }

    @Override
    public void addDictType (DictType dictType) throws BusinessException
    {
        if(getDictType (dictType.getDictTypeCode ())!=null)
        {
            throw new BusinessException ("新增失败，字典类型编码【"+dictType.getDictTypeCode ()+"】已经存在。");
        }
        m_dictMapper.addDictType (dictType);
    }

    @Override
    public List <DictType> findAllDictType ()
    {
        return m_dictMapper.findAllDictType ();
    }

    @Override
    public DictType getDictType (String dictTypeCode)
    {
        return m_dictMapper.getDictType (dictTypeCode);
    }

    @Override
    public void modifyDictType (DictType dictType)
    {
        m_dictMapper.modifyDictType (dictType);
    }

    @Override
    public void removeDictType (String dictTypeCode) throws BusinessException
    {
        if(findChildrenDict ("/", dictTypeCode).size ()>0)
        {
            throw new BusinessException ("编码类型【"+dictTypeCode+"】存在对应字典条目，请先移除对应字典条目后再执行此操作。");
        }
        m_dictMapper.removeDictType (dictTypeCode);
    }
}
